#import "SDAddCaseView.h"
#import "UIBarButtonItem+SDExtension.h"
#import "SDSelecterCaseTableViewCell.h"
#import "SDCaseTableViewController.h"
#import "SDCaseManageModel.h"
#import "SVProgressHUD.h"
@interface SDAddCaseView ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic,weak)UITableView * selecterCase;

@end

@implementation SDAddCaseView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showAddCaseVie];
}

- (void)showAddCaseVie{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"增加病例";
    
    self.navigationItem.leftBarButtonItem = [self leftBarAndRightBar:YES];
    
    self.navigationItem.rightBarButtonItem = [self leftBarAndRightBar:NO];
    
    UITableView * selecter =[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    [self.view addSubview:selecter];
    
    selecter.delegate = self;
    selecter.dataSource = self;
    selecter.rowHeight = UITableViewAutomaticDimension;
    selecter.estimatedRowHeight = 200;
    
    self.selecterCase = selecter;
    
    [selecter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.bottom.offset(0);
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" ];
    if (cell == nil){
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"疾病分类:";
            cell.detailTextLabel.text = self.caseManageModle.disease;
            
                   }else{
            cell.textLabel.text = @"疾病细分:";
            cell.detailTextLabel.text = self.caseManageModle.basic;
           
        }
        if (cell.detailTextLabel.text == nil ) {
            cell.detailTextLabel.text = @"请选择";
        }
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        
        if (indexPath.row == 0) {
            SDSelecterCaseTableViewCell * tempCell =[[SDSelecterCaseTableViewCell  alloc]newCellSelecter:YES caseModel:self.caseManageModle];
            
            cell = tempCell;

            

            
        }else{
            
            SDSelecterCaseTableViewCell * tempCell =[[SDSelecterCaseTableViewCell  alloc]newCellSelecter:NO caseModel:self.caseManageModle];
            

            cell = tempCell;

            tempCell.imgBlock = ^{
            
                //MARK: - 获取相册图片
                UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
                //类型
                pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //代理
                pickerController.delegate = self;
                //跳转
                [self presentViewController:pickerController animated:YES completion:nil];
            
            };
            
            tempCell.caseManGeModel = self.caseManageModle;
        }
        
        
    }
    
    return cell;
}

#pragma mark - PickerController获取图片的代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //MARK: - 取出从相册里选择的图片
    UIImage *selectImage = info[UIImagePickerControllerOriginalImage];
    //设置为添加图片按钮的背景图
    self.caseManageModle.imageIC = selectImage;
    //跳出
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self.selecterCase reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==0) {
        SDCaseTableViewController * sdcaseTable = [[SDCaseTableViewController alloc]init];
        
        if (indexPath.row == 0) {
            sdcaseTable.dOnBool = YES;
            [self.navigationController pushViewController:sdcaseTable animated:YES];
            
            
            
        }else{

            if (self.caseManageModle.disease == nil) {
                //MARK: - 弹窗
                [SVProgressHUD setMinimumDismissTimeInterval:1];
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                [SVProgressHUD showErrorWithStatus:@"请选择疾病分类"];          
                return;
            }
            
            sdcaseTable.dOnBool = NO;
            sdcaseTable.caseString = self.caseManageModle.disease;
            [self.navigationController pushViewController:sdcaseTable animated:YES];
        }
        sdcaseTable.strBoolk = ^(NSString * str){
            if (indexPath.row == 0) {
			
                self.caseManageModle.disease = str;

            }else{
                self.caseManageModle.basic = str;
            }
            [self.selecterCase reloadData];
        };
        sdcaseTable.upDataBool = ^(NSString * str){

        
        };
    }
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}



- (void)clickCommit{
    
    [self.selecterCase reloadData];
    
    if(self.caseManageModle.disease == nil){
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请选择疾病分类"];
        return;
    }
    if (self.caseManageModle.basic == nil) {
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请选择疾病细分"];
        return;
    }
    //MARK: - 获取当前时间
    NSDate * date = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    self.caseManageModle.timeString = [format stringFromDate:date];
    NSLog(@"%@",self.caseManageModle.array);
    _modeBlook(self.caseManageModle);
    

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickLeftBar{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (UIBarButtonItem *)leftBarAndRightBar:(BOOL)numBool{
    if (numBool) {
        UIBarButtonItem * temp = [UIBarButtonItem initWithImageStr:@"JianTou" addTarget:self withAction:@selector(clickLeftBar)];
        return temp;
    }else{
        
        UIBarButtonItem * temp = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(clickCommit)];
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
        return temp;
    }
}

-(SDCaseManageModel *)caseManageModle{

    if (_caseManageModle ==nil) {
        _caseManageModle = [[SDCaseManageModel alloc]init];
    }

    return _caseManageModle;
}

@end
